import QtQuick 2.14
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.14
import QtQuick.Dialogs 1.2
import PageService 1.0

Window {
    id: mainWin
    visible: true
    width: 640
    height: 480

    property alias pageNumText: pageNumLabel.text

    ColumnLayout {
        anchors.centerIn: parent

        Image {
            id: pageImg
            source: "qrc:/img/test_2.png"

            Layout.preferredHeight: mainWin.height / 2
            Layout.preferredWidth: mainWin.width / 2
            height: mainWin.height / 2
            width: mainWin.width / 2

            fillMode: Image.PreserveAspectCrop

            MouseArea {
                anchors.fill: parent

                onClicked: {
                    console.log("点击了一张图片")
                    pageNumText = ""
                    saveDialog.visible = true
                }
            }
        }

        Label {
            id: pageNumLabel
            text: "请选择一张图片识别"
            width: parent.width

            Layout.alignment: Qt.AlignHCenter
        }
    }

    FileDialog{
        id: saveDialog
        title: "打开图片"
        folder: shortcuts.home
        selectFolder: false
        selectExisting: true
        nameFilters: ["Image files (*.jpg)", "Image files (*.png)", "Image files (*.bmp)"]
        visible: false
        onAccepted: {
            console.log("You choose: " + saveDialog.fileUrls);
            pageImg.source = saveDialog.fileUrls[0]
            PageService.start(saveDialog.fileUrls[0])
        }

        onRejected: {
            console.log("Canceled")
        }
    }

    Connections {
        target: PageService
        onEnd: {
            pageNumText = "curPage is " + pageNum
            console.log("curPage is " + pageNum)
        }
    }
}
